package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Notice;
import com.ruoyi.business.domain.bo.NoticeBo;
import com.ruoyi.business.domain.vo.NoticeVo;
import com.ruoyi.business.mapper.NoticeMapper;
import com.ruoyi.business.service.INoticeService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 最爱喝兽奶
 * @create 2022/9/21
 **/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {


    @Override
    public TableDataInfo<NoticeVo> queryNoticeByPage(NoticeBo bo) {
        Page<NoticeVo> result = baseMapper.queryNoticeByPage(PageUtils.buildPage(bo), bo);
        return PageUtils.buildTableDataInfo(result);
    }

    @Override
    public void deleteById(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void updateNoticeById(NoticeBo bo) {
        Notice notice = new Notice();
        BeanUtils.copyBeanProp(notice, bo, true);
        baseMapper.updateById(notice);
    }

    @Override
    public void addNotice(NoticeBo bo) {
        Notice notice = new Notice();
        BeanUtils.copyBeanProp(notice, bo, true);
        // 添加告示时间
        notice.setCreateTime(new Date());
        // 查询当前告示的最大的排序
        Integer sort = baseMapper.selectMaxSort();
        notice.setSort(sort + 1);
        baseMapper.insert(notice);
    }

    @Override
    public NoticeVo getNoticeById(String id) {
        Notice notice = baseMapper.selectById(id);
        if (null == notice) {
            return null;
        }
        NoticeVo vo = new NoticeVo();
        BeanUtils.copyBeanProp(vo, notice, true);
        return vo;
    }

    @Override
    public void moveUp(String id) {
        Notice notice = baseMapper.selectById(id);
        if (null == notice) {
            throw new RuntimeException("告示序列有错误，请删除之后，重新添加");
        }
        // 查询当前告示的上一条告示
        Notice pre = baseMapper.selectPreById(notice);
        if (null == pre) {
            throw new RuntimeException("当前已经是第一条告示了");
        }
        swapNotice(notice, pre);
    }

    @Override
    public void moveDown(String id) {
        Notice notice = baseMapper.selectById(id);
        if (null == notice) {
            throw new RuntimeException("告示序列有错误，请删除之后，重新添加");
        }
        // 查询当前告示的下一条告示
        Notice next = baseMapper.selectNextById(notice);
        if (null == next) {
            throw new RuntimeException("当前已经是最后一条告示了");
        }
        swapNotice(notice, next);
    }

    /**
     * 交换两条告示的sort值
     * @param notice
     * @param pre
     */
    private void swapNotice(Notice notice, Notice pre) {
        Integer temp = notice.getSort();
        notice.setSort(pre.getSort());
        pre.setSort(temp);
        baseMapper.updateById(notice);
        baseMapper.updateById(pre);
    }
}
